package export;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * This class provides methods for the csv export
 * @author Kevin Wagner
 * @version 1.0
 */
public class CSVExport {
	
	/**
	 * Exports the given result to the file result.txt.
	 * 
	 * @param result
	 * @param postComma 
	 * @throws Exception 
	 */
	public static void exportResult(ArrayList<double[]> result, int postComma) throws Exception {
			// Create file
			FileWriter fstream = new FileWriter("result.csv");
			BufferedWriter out = new BufferedWriter(fstream);
			String format = "##0.";
			if(postComma<0){
				postComma=5;
			}
			for (int i = 0; i < postComma; i++) {
				format += "0";
			}
			DecimalFormat df = new DecimalFormat(format);
			out.write("Gen;Best;Worst;Mean;Std");
			out.newLine();
			for (int i = 0; i < result.size(); i++) {
				out.write(i+";");
				for(int j=0;j<result.get(i).length;j++){
					out.write(df.format(result.get(i)[j])+";");
				}
				out.newLine();
			}
			out.flush();
			// Close the output stream
			out.close();
	}

}
